package com.example.yichuangyun.mvvm.di.retrofit;

import com.example.yichuangyun.model.StatusModel;
import com.example.yichuangyun.model.UserInfoModel;
import com.example.yichuangyun.model.business.LabelModel;
import com.example.yichuangyun.model.call.CallRecordTransModel;
import com.example.yichuangyun.model.company.AreaModel;
import com.example.yichuangyun.model.company.CollectDataModel;
import com.example.yichuangyun.model.company.CompanyDataModel;
import com.example.yichuangyun.model.company.CompanyDetailsContactsDataModel;
import com.example.yichuangyun.model.company.CustomInfoDataModel;
import com.example.yichuangyun.model.company.GsBaseInfoModel;
import com.example.yichuangyun.model.company.GsChangeRecordDataModel;
import com.example.yichuangyun.model.company.IntellectualPropertyDataModel;
import com.example.yichuangyun.model.company.ManageInfoDataModel;
import com.example.yichuangyun.model.company.OutsideInvestDataModel;
import com.example.yichuangyun.model.company.RiskInfoDataModel;
import com.example.yichuangyun.model.contacts.AddContactsParamsModel;
import com.example.yichuangyun.model.contacts.AddressDataModel;
import com.example.yichuangyun.model.contacts.ContactsBusinessDataModel;
import com.example.yichuangyun.model.contacts.ContactsTagModel;
import com.example.yichuangyun.model.order.BusinessRecordModel;
import com.example.yichuangyun.model.order.DefPackageModel;
import com.example.yichuangyun.model.order.DefPriceModel;
import com.example.yichuangyun.model.order.FileInfoDataModel;
import com.example.yichuangyun.model.order.FileTypeModel;
import com.example.yichuangyun.model.order.IncomePayDataModel;
import com.example.yichuangyun.model.order.InvoiceDataModel;
import com.example.yichuangyun.model.order.OrderFollowDataModel;
import com.example.yichuangyun.model.order.OrderListDataModel;
import com.example.yichuangyun.model.order.OrderListParamsModel;
import com.example.yichuangyun.model.order.SelectOrderInfoDataModel;
import com.example.yichuangyun.model.order.ServiceRecordModel;
import com.example.yichuangyun.model.order.VisitRecordsModel;
import com.example.yichuangyun.utils.treelist.SetShareBean;

import org.json.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class RetrofitHelper {
    private final ApiConstants mApiConstants;

    public RetrofitHelper(ApiConstants paramApiConstants) {
        this.mApiConstants = paramApiConstants;
    }

    public static <T> FlowableTransformer<T, T> appointThread() {
        return upstream -> upstream.subscribeOn(Schedulers.io())
                // 使用observerOn()指定订阅者运行的线程
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 联系人列表
     */
    public Flowable<BaseModel<AddressDataModel>> getContactsList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getContactsList(requestBody).compose(appointThread());
    }

    /**
     * 上传图片
     */
    public Flowable<BaseModel<String>> uploadImage(File file) {
        MultipartBody.Part part = null;
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        try {
            // 路径有中文名要进行编码
            part = MultipartBody.Part.createFormData("file", URLEncoder.encode(file.getName(), "utf-8"), requestBody);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return mApiConstants.uploadImage(part).compose(appointThread());
    }

    /**
     * 上传图片
     */
    public Flowable<StatusModel> uploadImage2(File file) {
        MultipartBody.Part part = null;
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        try {
            // 路径有中文名要进行编码
            part = MultipartBody.Part.createFormData("file", URLEncoder.encode(file.getName(), "utf-8"), requestBody);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return mApiConstants.uploadImage2(part).compose(appointThread());
    }

    /**
     * 组织架构
     */
    public Flowable<SetShareBean> getTreeData(JSONObject jsonObject) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        return mApiConstants.getTreeData(requestBody).compose(appointThread());
    }

    /**
     * 联系人来源
     */
    public Flowable<BaseModel<List<LabelModel>>> getContactsSource() {
        return mApiConstants.getContactsSource().compose(appointThread());
    }

    /**
     * 标签
     */
    public Flowable<BaseModel<List<ContactsTagModel>>> getTags(JSONObject jsonObject) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        return mApiConstants.getTags(requestBody).compose(appointThread());
    }

    /**
     * 联系人类型
     */
    public Flowable<BaseModel<List<LabelModel>>> getContactsType() {
        return mApiConstants.getContactsType().compose(appointThread());
    }

    /**
     * 添加联系人
     */
    public Flowable<BaseModel<String>> saveContractInfo(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.saveContractInfo(requestBody).compose(appointThread());
    }

    /**
     * 编辑联系人
     */
    public Flowable<BaseModel<String>> editContractInfo(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.editContractInfo(requestBody).compose(appointThread());
    }

    /**
     * 获取历史标签
     */
    public Flowable<BaseModel<List<ContactsTagModel>>> getAllHistoryTags(int id) {
        return mApiConstants.getUserAllTags(id).compose(appointThread());
    }

    /**
     * 删除标签
     */
    public Flowable<BaseModel<String>> deleteTag(String id) {
        return mApiConstants.deleteTag(id).compose(appointThread());
    }

    /**
     * 删除历史标签 -- 用户所有
     */
    public Flowable<BaseModel<String>> deleteAllTag(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.deleteAllTag(requestBody).compose(appointThread());
    }

    /**
     * 查询联系人 -- 商机记录
     */
    public Flowable<BaseModel<ContactsBusinessDataModel>> getContactsBusinessList(String contactId, int examine) {
        return mApiConstants.getContactsBusinessList(contactId, 1, 100, examine).compose(appointThread());
    }

    /**
     * 联系人详情
     */
    public Flowable<BaseModel<AddContactsParamsModel>> getContactsDetails(String contactId, int examine) {
        return mApiConstants.getContactsDetails(contactId, contactId, examine).compose(appointThread());
    }

    /**
     * 获取所有区域
     */
    public Flowable<BaseModel<List<AreaModel>>> getAreaList() {
        return mApiConstants.getAreaList().compose(appointThread());
    }

    /**
     * 获取企业列表
     */
    public Flowable<BaseModel<CompanyDataModel>> getCompanyList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getCompanyList(requestBody).compose(appointThread());
    }

    /**
     * 获取官方信息详情 基本信息
     */
    public Flowable<BaseModel<CompanyDataModel.CompanyModel>> getOfficialBaseInfoDetail(String eid) {
        return mApiConstants.getOfficialBaseInfoDetail(eid).compose(appointThread());
    }

    /**
     * 获取官方信息详情 基本信息
     */
    public Flowable<BaseModel<CompanyDetailsContactsDataModel>> getDetailsContacts(String eid) {
        return mApiConstants.getDetailsContacts(eid).compose(appointThread());
    }

    /**
     * 工商信息 基本信息
     */
    public Flowable<BaseModel<GsBaseInfoModel>> getGsBaseInfo(String eid) {
        return mApiConstants.getGsBaseInfo(eid).compose(appointThread());
    }

    /**
     * 工商变更记录
     */
    public Flowable<BaseModel<GsChangeRecordDataModel>> getGsChangeRecord(String eid) {
        return mApiConstants.getGsChangeRecord(eid, 1, 100).compose(appointThread());
    }

    /**
     * 风险信息
     */
    public Flowable<BaseModel<RiskInfoDataModel>> getRiskInfo(String eid) {
        return mApiConstants.getRiskInfo(eid).compose(appointThread());
    }

    /**
     * 经营信息
     */
    public Flowable<BaseModel<ManageInfoDataModel>> getManageInfo(String eid) {
        return mApiConstants.getManageInfo(eid).compose(appointThread());
    }

    /**
     * 产权证书
     */
    public Flowable<BaseModel<IntellectualPropertyDataModel>> getIntellectualProperty(String eid) {
        return mApiConstants.getIntellectualProperty(eid).compose(appointThread());
    }

    /**
     * 对外投资
     */
    public Flowable<BaseModel<List<OutsideInvestDataModel.OutsideInvestModel>>> getInvestment(String eid) {
        return mApiConstants.getInvestment(eid).compose(appointThread());
    }

    /**
     * 收藏企业
     */
    public Flowable<BaseModel<String>> collectCompany(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.collectCompany(requestBody).compose(appointThread());
    }

    /**
     * 取消收藏企业
     */
    public Flowable<BaseModel<String>> cancelCollectCompany(String eid) {
        return mApiConstants.cancelCollectCompany(eid).compose(appointThread());
    }

    /**
     * 获取可拨打电话
     */
    public Flowable<BaseModel<List<CompanyDetailsContactsDataModel.ContactsModel>>> getContacts(String eid) {
        return mApiConstants.getContacts(eid, eid).compose(appointThread());
    }

    /**
     * 获取我的收藏列表
     */
    public Flowable<BaseModel<CollectDataModel>> getMyCollectList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getMyCollectList(requestBody).compose(appointThread());
    }

    /**
     * 获取我的收藏列表
     */
    public Flowable<BaseModel<String>> delCollectCompany(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.delCollectCompany(requestBody).compose(appointThread());
    }

    /**
     * 完善企业信息
     */
    public Flowable<BaseModel<String>> saveCompanyInfo(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.saveCompanyInfo(requestBody).compose(appointThread());
    }

    /**
     * 多图上传
     */
    public Flowable<BaseModel<List<String>>> uploadImage(MultipartBody.Part[] parts) {
        return mApiConstants.uploadImage(parts).compose(appointThread());
    }

    /**
     * 自主信息列表
     */
    public Flowable<BaseModel<CustomInfoDataModel>> getCustomList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getCustomList(requestBody).compose(appointThread());
    }

    /**
     * 获取订单信息
     */
    public Flowable<BaseModel<SelectOrderInfoDataModel>> selectOrderInfo(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.selectOrderInfo(requestBody).compose(appointThread());
    }

    /**
     * 联系人列表
     */
    public Flowable<BaseModel<OrderListDataModel>> getOrderList(OrderListParamsModel model) {
        return mApiConstants.getOrderList(model.getCurrent(), 10, model.getKeyValue(),
                        model.getRenewalState(), model.getPromotionId(), model.getUserId(), model.getIsDept(),
                        model.getOrderStatus(), model.getType(), model.getStartTime(), model.getEndTime(),
                        model.getMoneyConfirmStatus(), model.getServiceCategory())
                .compose(appointThread());
    }

    /**
     * 款项确认
     */
    public Flowable<BaseModel<String>> moneyConfirm(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.moneyConfirm(requestBody).compose(appointThread());
    }

    /**
     * 订单详情
     */
    public Flowable<BaseModel<OrderListDataModel.OrderRecordsModel>> getOrderDetail(String id) {
        return mApiConstants.getOrderDetails(id).compose(appointThread());
    }

    /**
     * 订单详情-服务
     */
    public Flowable<BaseModel<ServiceRecordModel>> getOrderDetailsForService(String id) {
        return mApiConstants.getOrderDetailsForService(id, 99).compose(appointThread());
    }

    /**
     * 订单详情-商机
     */
    public Flowable<BaseModel<BusinessRecordModel>> getOrderDetailsForBusiness(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getOrderDetailsForBusiness(requestBody).compose(appointThread());
    }

    /**
     * 订单详情-订单跟进
     */
    public Flowable<BaseModel<OrderFollowDataModel>> getOrderDetailsForOrderFollow(String id) {
        return mApiConstants.getOrderDetailsForOrderFollow(id).compose(appointThread());
    }

    /**
     * 订单详情-客户回访
     */
    public Flowable<BaseModel<VisitRecordsModel>> getOrderDetailsForVisit(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getOrderDetailsForVisit(requestBody).compose(appointThread());
    }

    /**
     * 订单详情-文件信息
     */
    public Flowable<BaseModel<FileInfoDataModel>> getOrderDetailsForFileInfo(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getOrderDetailsForFileInfo(requestBody).compose(appointThread());
    }

    /**
     * 订单详情-发票记录
     */
    public Flowable<BaseModel<InvoiceDataModel>> getOrderDetailsForInvoice(String id) {
        return mApiConstants.getOrderDetailsForInvoice(id, 1, 50, -1).compose(appointThread());
    }

    /**
     * 订单详情-收支记录
     */
    public Flowable<BaseModel<IncomePayDataModel>> getOrderDetailsForIncomePay(String id) {
        return mApiConstants.getOrderDetailsForIncomePay(id).compose(appointThread());
    }

    /**
     * 添加跟进-获取附件类型
     */
    public Flowable<List<FileTypeModel>> getFileType() {
        return mApiConstants.getFileType().compose(appointThread());
    }

    /**
     * 订单跟进
     */
    public Flowable<StatusModel> addOrderFollow(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.addOrderFollow(requestBody).compose(appointThread());
    }

    /**
     * 修改头像
     */
    public Flowable<BaseModel<String>> editThumbAvatar(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.editThumbAvatar(requestBody).compose(appointThread());
    }

    /**
     * 个人信息
     */
    public Flowable<BaseModel<UserInfoModel>> getUserInfo(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getUserInfo(requestBody).compose(appointThread());
    }

    /**
     * 获取默认套餐
     */
    public Flowable<BaseModel<List<DefPackageModel>>> getDefaultPriceResp(String id) {
        return mApiConstants.getDefaultPriceResp(id).compose(appointThread());
    }

    /**
     * 判断有无套餐，获取价格
     */
    public Flowable<BaseModel<DefPriceModel>> getPrice(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getPrice(requestBody).compose(appointThread());
    }

    /**
     * 生成订单
     */
    public Flowable<BaseModel<String>> createOrder(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.createOrder(requestBody).compose(appointThread());
    }

    /**
     * 录音文件
     */
    public Flowable<BaseModel<String>> getRecordUrl(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getRecordUrl(requestBody).compose(appointThread());
    }

    /**
     * 录音文件
     */
    public Flowable<BaseModel<String>> getPcRecordUrl(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getPcRecordUrl(requestBody).compose(appointThread());
    }

    /**
     * 录音文件转文字
     */
    public Flowable<BaseModel<CallRecordTransModel>> getTranscription(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getTranscription(requestBody).compose(appointThread());
    }

    /**
     * 获取企业的eid
     */
    public Flowable<BaseModel<CompanyDataModel.CompanyModel>> getEid(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getEid(requestBody).compose(appointThread());
    }
}
